package com.elitel.frame.base.service.impl;

import com.elitel.frame.base.service.HiveService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class HiveServiceImpl implements HiveService {
    private static final Logger logger = LoggerFactory.getLogger(HiveServiceImpl.class);

    @Autowired(required = false)
    @Qualifier("hiveJdbcTemplate")
    JdbcTemplate hiveJdbcTemplate;

    @Override
    public List<Map<String, Object>> selectTable(String sql) {
        long starttime = System.currentTimeMillis();
        List<Map<String, Object>> maps = hiveJdbcTemplate.queryForList(sql);
        logger.info("查询SQL：{} ,耗时：{}秒", sql, (double) (System.currentTimeMillis() - starttime) / 1000);
        return maps;
    }

    @Override
    public Map<String, Object> queryForMap(String sql) {
        long starttime = System.currentTimeMillis();
        Map<String, Object> map = hiveJdbcTemplate.queryForMap(sql);
        logger.info("查询SQL：{} ,耗时：{}秒", sql, (double) (System.currentTimeMillis() - starttime) / 1000);
        return map;
    }

    @Override
    public void execute(String sql) {
        hiveJdbcTemplate.execute(sql);
        logger.info("查询SQL：{}", sql);
    }
}
